package com.android.geotrack;

import java.util.LinkedList;

import com.android.geotrack.user.User;
import com.android.geotrack.user.UserActivity;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * "Adapter" for the GridView in the "Track"-tab.
 * Loads a clickable thumbnail for each user in our database.
 * 
 * @author	MuMe09 Team 6
 */
public class TrackAdapter extends BaseAdapter
{
    private Context context;
    private LinkedList<User> users;
    private LinkedList<Drawable> thumbs;
    
    /**
     * Default BaseAdapter constructor.
     * 
     * @param 	context
     * @param 	users
     * @param 	thumbs
     */
    public TrackAdapter(Context context, Object users, Object thumbs)
    {
        this.context = context;
        this.users = (LinkedList<User>) users;
        this.thumbs = (LinkedList<Drawable>) thumbs;
    }

    /**
     * Returns the number of thumbnails.
     */
	public int getCount() {
        return thumbs.size();
    }

	/**
	 * 
	 */
    public Object getItem(int position)
    {
        return null;
    }

    public long getItemId(int position)
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        
        // Click on imageView
        final int pos = position;
        imageView.setOnClickListener(new ImageView.OnClickListener(){
        	public void onClick(View v) {
        		Intent i = new Intent(context, UserActivity.class);
        		i.putExtra("user", users.get(pos).getName());
        		i.putExtra("distance", Double.toString(users.get(pos).getDistance()));
        		i.putExtra("age", Integer.toString(users.get(pos).getAge()));
        		i.putExtra("score", Double.toString(users.get(pos).getScore()));
        		i.putExtra("image", users.get(pos).getThumbnail_large());
        		i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
        		context.startActivity(i);
        	}	
        });
        
        imageView.setImageDrawable(thumbs.get(position));
        
        return imageView;
    }
}